# Copyright 2014-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if {[skip_hw_breakpoint_tests]} {
    return 0
}

standard_testfile

if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile}]} {
    return -1
}

if ![runto_main] {
    fail "Can't run to main"
    return -1
}

# If we can read the memory at address 0, skip the test.
if { [is_address_zero_readable] } {
    untested "Memory at address 0 is readable"
    return
}

delete_breakpoints

# Test whether the target supports hardware breakpoints at all.

set supports_hbreak 0
set test "probe hardware breakpoint support"
gdb_test_multiple "hbreak main" $test {
    -re "No hardware breakpoint support in the target.*$gdb_prompt $" {
	pass $test
    }
    -re "Hardware breakpoints used exceeds limit.*$gdb_prompt $" {
	pass $test
    }
    -re "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*$gdb_prompt $" {
	set supports_hbreak 1
	pass $test
    }
}

if {!$supports_hbreak} {
    unsupported "hardware breakpoints"
    return
}

delete_breakpoints

# Force immediate breakpoint insertion.
gdb_test_no_output "set breakpoint always-inserted on"

# Hardware breakpoints are implemented using a mechanism that is not
# dependent on being able to modify the target's memory, we should be
# able to set them even in unmapped memory areas.
gdb_test "hbreak *0" "Hardware assisted breakpoint \[0-9\]+ at 0x0"

gdb_test "info break" "hw breakpoint.*y.*0x0\+\[ \t\]\+" \
    "info break shows hw breakpoint"

gdb_test_no_output "delete \$bpnum" "" "delete" \
    "delete hw breakpoint"

gdb_test "info break" "No breakpoints or watchpoints\." \
    "info break shows no breakpoints"
